package test.jgroups;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.util.UUID;
import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;

public class MessageFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private final static List<MessageFrame> fremes = new LinkedList<MessageFrame>();;
	JTextArea viewArea;
	JTextField inputField;
	JButton sendButton;
	private JChannel channel;

	public MessageFrame(String props, String groupName,String myName) throws Exception {
		super("JGroups Chatting");
		channel = new JChannel(props);
		channel.setName(myName);
//		jChannel.setAddressGenerator(new MyAddressGenerator("liuxyz", "0.0.0.0"));
		channel.connect(groupName);
		channel.setReceiver(new ReceiverAdapter() {
			@Override
			public void receive(Message msg) {
				viewArea.append(channel.getName(msg.getSrc()) + " say:" + msg.getObject().toString() + "\n");
			}
			@Override
			public void viewAccepted(View view) {
				System.out.println(view.getMembers().get(0).getClass());
				viewArea.append("new member join or level!! size:" + view.getMembers().size() + "\n");
			}
		});
		initComponents();
		fremes.add(this);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fremes.remove(MessageFrame.this);
				if (fremes.isEmpty())
					System.exit(0);
			}
		});
		this.setSize(280, 250);
		this.setVisible(true);
		this.setAlwaysOnTop(true);
	}

	public MessageFrame(String groupName,String myName) throws Exception {
		this(JChannel.DEFAULT_PROTOCOL_STACK, groupName,myName);
	}

	public void initComponents() {
		viewArea = new JTextArea();
		inputField = new JTextField();
		sendButton = new JButton("send");

		viewArea.append("sign in,myname:"+channel.getName()+",size:" + channel.getView().getMembers().size() + "\n");

		ActionListener al = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == inputField || e.getSource() == sendButton) {
					Message msg = new Message(null, null, inputField.getText());
					inputField.setText("");
					try {
						channel.send(msg);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}
		};
		inputField.addActionListener(al);
		sendButton.addActionListener(al);

		JPanel p = new JPanel(new BorderLayout(5, 5));
		p.add(inputField, BorderLayout.CENTER);
		p.add(sendButton, BorderLayout.EAST);

		add(viewArea);
		add(p, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					int x = 20;
					int y = 40;
					new MessageFrame("helloworld","sky").setLocation(x, y);
					new MessageFrame("helloworld","cup").setLocation(x += 280, y += 40);
					new MessageFrame("helloworld","king").setLocation(x += 280, y += 40);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
